package net.wofly.common.pay.wxpay.service;

import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.wx.api.WxPayConfigStorage;
import com.egzosn.pay.wx.api.WxPayService;
import com.egzosn.pay.wx.bean.WxTransactionType;
import net.wofly.cashier.order.util.PayType;
import net.wofly.common.pay.AbstractPayProcessor;
import net.wofly.common.pay.PayContext;
import net.wofly.common.pay.util.ClientType;
import net.wofly.common.pay.wxpay.config.WxPayConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Map;

@Service
public class WxPayProcessor extends AbstractPayProcessor {
    
    //ssl 退款证书相关 不使用可注释
    private static String KEYSTORE = "ssl 退款证书";
    private static String STORE_PASSWORD = "ssl 证书对应的密码， 默认为商户号";
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    WxPayConfig wxPayConfig;
    private PayService service = null;
    
    public void initConfig(Integer clientType) {
        WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
        wxPayConfigStorage.setMchId(wxPayConfig.getMchID());
        wxPayConfigStorage.setAppid(wxPayConfig.getAppID());
        wxPayConfigStorage.setKeyPublic(wxPayConfig.getKey_public());
        wxPayConfigStorage.setSecretKey(wxPayConfig.getKey_private());
        wxPayConfigStorage.setNotifyUrl(wxPayConfig.getNotify_url_pc());
        wxPayConfigStorage.setReturnUrl(wxPayConfig.getReturn_url_pc());
        if (ClientType.移动端_H5.getValue().intValue() == clientType) {
            wxPayConfigStorage.setReturnUrl(wxPayConfig.getReturn_url_h5());
        }
        wxPayConfigStorage.setSignType(wxPayConfig.getSign_type());
        wxPayConfigStorage.setInputCharset("utf-8");
        
        service = new WxPayService(wxPayConfigStorage);
        
        HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
        
        //ssl 退款证书相关 不使用可注释
        if (!"ssl 退款证书".equals(KEYSTORE)) {
            httpConfigStorage.setKeystore(KEYSTORE);
            httpConfigStorage.setStorePassword(STORE_PASSWORD);
            httpConfigStorage.setPath(true);
        }
        
        
        //请求连接池配置
        //最大连接数
        httpConfigStorage.setMaxTotal(20);
        //默认的每个路由的最大连接数
        httpConfigStorage.setDefaultMaxPerRoute(10);
        service.setRequestTemplateConfigStorage(httpConfigStorage);
    }
    
    @Override
    public PayContext preCheck(PayContext payContext) {
        return null;
    }
    
    @Override
    public PayContext doPay(PayContext payContext) throws IOException {
        logger.info(">>> 微信支付...");
        initConfig(payContext.getClientType());
        PayOrder payOrder = payContext.getPayOrder();
        Map orderInfo = service.orderInfo(payOrder);
        if (payOrder.getTransactionType().equals(WxTransactionType.NATIVE)) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(service.genQrPay(payOrder), "JPEG", baos);
            payContext.setContent(Base64.getEncoder().encodeToString(baos.toByteArray()));
        } else {
            payContext.setContent(service.buildRequest(orderInfo, MethodType.POST));
        }
        return payContext;
    }
    
    @Override
    public Integer getPayChannel() {
        return PayType.微信支付.getValue();
    }
}
